﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPLDB.Utility.Extensions;
using WPLDB.DA.Interfaces;
using WPLDB.DA.Models;
using WPLDB.DA.Interfaces.UI.Models;

namespace WPLDB.Code
{
    /// <summary>
    /// This class helps to created Entity Class C# code 
    /// and variables naming are not explicit required the explanations
    /// </summary>
    public class CreateEntityClassCode : ICreateClassCode
    {

        #region "Variables"
        string _className;
        IList<TableModel> _listOfTables;
        #endregion

        #region "ctor"

        public CreateEntityClassCode(string className, IList<TableModel> listOfTables)
        {
            _className = className.RemoveSpecialChar();
            _listOfTables = listOfTables;
        }

        #endregion

        #region "Constants"

        string CLASS = "[NEWLINE][T]public class [CLASSNAME] : DataContext[NEWLINE]" +
            "[T]{[NEWLINE]" +
            "[T][T]public [CLASSNAME](string connnectionString) : base(connnectionString) { }[NEWLINE]" +
            "[NEWLINE]" +
            "[LISTOFPROPERTIES][NEWLINE]" +
            "[T]}[NEWLINE][NEWLINE]";

        string PROPERTY = "[T][T]public Table<[PROPERTYSINGULAR]> [PROPERTYPLURAL];[NEWLINE]";

        #endregion

        #region "Interface Implementation"

        public string GenerateClassString()
        {
            var properties = new StringBuilder();

            if (null != _listOfTables)
                foreach (var item in _listOfTables)
                    if (item.Name.Original.IsNotNullOrNotEmpty())
                        properties.Append(PROPERTY
                            .ReplacePropertySingular(item.Name.Singular)
                            .ReplacePropertyPlural(item.Name.Plural)
                            .ReplaceNewLine());



            return CLASS
                .ReplaceClass(_className)
                .ReplaceListOfProperties(properties.ToString())
                .ReplaceNewLine();


        }

        #endregion

    }
}
